package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.Pages;
import com.zjuee.base.dto.common.plan.PlanCarryDto;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.base.enums.RedisCacheType;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.translation.utils.CaseUtil;
import com.zjuee.service.PlanCarryService;
import com.zjuee.vo.PlanCarryVo;
import com.zjuee.base.model.common.plan.PlanCarry;
import com.zjuee.web.ledger.mapper.PlanCarryMapper;
import com.zjuee.web.ledger.mapper.PlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author ding jinli
 * @since 1.0
 */
@Slf4j
@Service
public class PlanCarryServiceImpl implements PlanCarryService {
    @Resource
    private TranslateData translateData;

    @Resource
    private PlanCarryMapper planCarryMapper;
    @Resource
    private PlanMapper planMapper;

    @Override
    public Long add(PlanCarry type) {
        int state = planCarryMapper.insert(type);
        if (1 == state) {
            return type.getId();
        }
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        PlanCarry planCarry = planCarryMapper.selectById(id);
        if (validateId.equals(planCarry.getWpiId())) {
            int type = planCarryMapper.deleteById(id);
            if (1 == type) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(PlanCarry type) {
        return false;
    }

    @Override
    public PlanCarry selectById(Long id, Long validateId) {
        PlanCarry planCarry = planCarryMapper.selectById(id);
        if (validateId.equals(planCarry.getWpiId())) {
            return planCarryMapper.selectById(id);
        }
        return null;
    }

    @Override
    public Page<PlanCarry> list(PlanCarryDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<PlanCarry> fuzzyQuery(PlanCarryDto dto) {
        return null;
    }

    @Override
    public List<PlanCarry> findPlanCarry(PlanCarry planCarry) {
        if (planCarry == null) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (planCarry.getBciId() != null) {
            queryWrapper.eq("bci_id", planCarry.getBciId());
        }
        if (planCarry.getWpcModel() != null) {
            queryWrapper.eq("wpc_model", planCarry.getWpcModel());
        }
        if (planCarry.getWpiId() != null) {
            queryWrapper.eq("wpi_id", planCarry.getWpiId());
        }
        if (planCarry.getWpcType() != null) {
            queryWrapper.eq("wpc_type", planCarry.getWpcType());
        }
        if (planCarry.getWpcScode() != null) {
            queryWrapper.eq("wpc_scode", planCarry.getWpcScode());
        }
        if (planCarry.getWpcName() != null) {
            queryWrapper.eq("wpc_name", planCarry.getWpcName());
        }
        if (planCarry.getWpcSource() != null) {
            queryWrapper.eq("wpc_source", planCarry.getWpcSource());
        }

        return planCarryMapper.selectList(queryWrapper);
    }

    @Override
    public List<PlanCarry> findPlanCarrylistByBciId(Long bciId, Integer year) {
        return planMapper.findPlanCarrylistByBciId(bciId, year);
    }

    @Override
    public String getCompanyNameById(Long bciId) {
        return planCarryMapper.getCompanyNameById(bciId);
    }

    @Override
    public Page<PlanCarryVo> findPlanCarryCoHome(PlanCarryDto planCarryDto) {
        if (!Optional.ofNullable(planCarryDto.getPage()).isPresent()) {
            planCarryDto.setPage(new Pages());
        }
        Page<PlanCarryVo> page = planCarryMapper.listPagePlanCarry(planCarryDto.getPage().convert(), planCarryDto);
        translate(page.getRecords(),"list");
        return page;
    }

    public Object translate(Object list, String returnType) {
        Object obj = translateData.
                translateData(list, "planCarryCoHomeHandle",
                        String.valueOf(RedisCacheType.DISPOSE_TYPE),
                        String.valueOf(RedisCacheType.DISPOSE_STYPE),
                        String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue()),
                        String.valueOf(RedisCacheType.SOLID_TYPE),
                        String.valueOf(RedisCacheType.SOLID_STYPE),
                        String.valueOf(RedisCacheType.DANGER_SCLASS));
        if (StringUtils.equalsIgnoreCase(returnType, "map")) {
            return obj;
        }
        return CaseUtil.castList(obj, PlanCarryVo.class);
    }
}
